package com.java110.core.event.cmd;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.store.StoreDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import org.slf4j.Logger;
import com.java110.core.log.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


public abstract class Cmd implements ServiceCmdListener {

    private static Logger logger = LoggerFactory.getLogger(Cmd.class);

    @Autowired(required = false)
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Autowired
    private IStoreV1InnerServiceSMO storeV1InnerServiceSMOImpl;

    /**
     * 分页信息校验
     *
     * @param reqJson
     */
    protected void validatePageInfo(JSONObject reqJson) {
        Assert.jsonObjectHaveKey(reqJson, "page", "请求中未包含page信息");
        Assert.jsonObjectHaveKey(reqJson, "row", "请求中未包含row信息");
    }

    /**
     * 分页信息校验
     *
     * @param context
     */
    protected void validateAdmin(ICmdDataFlowContext context) {
        String storeTypeCd = CmdContextUtils.getStoreTypeCd(context);
        if(!StoreDto.STORE_TYPE_ADMIN.equals(storeTypeCd)){
            throw new CmdException("该接口只能运营团队调用");
        }
    }

    /**
     * 分页信息校验
     *
     * @param context
     */
    protected void validateShop(ICmdDataFlowContext context) {
        String storeTypeCd = CmdContextUtils.getStoreTypeCd(context);
        if(!StoreDto.STORE_TYPE_MALL.equals(storeTypeCd)){
            throw new CmdException("该接口只能店铺调用");
        }
    }
//
//    @Override
//
//    public void cmd(CmdEvent event) throws CmdException {
//        //这里处理业务逻辑数据
//        ICmdDataFlowContext dataFlowContext = event.getCmdDataFlowContext();
//        //获取请求数据
//        JSONObject reqJson = dataFlowContext.getReqJson();
//
//        logger.debug("API服务 --- 请求参数为：{}", reqJson.toJSONString());
//
//        validate(event, dataFlowContext,reqJson);
//
//        doCmd(event, dataFlowContext, reqJson);
//
//        //logger.debug("API服务 --- 返回报文信息：{}", dataFlowContext.getResponseEntity());
//    }

    /**
     * 查询用户信息
     *
     * @param context
     * @return
     */
    protected UserDto getUserDto(ICmdDataFlowContext context) {
        String userId = CmdContextUtils.getLoginUserId(context);
        Assert.hasLength(userId, "用户未登录");
        if (userId.startsWith("-")) {
            throw new IllegalArgumentException("用户未登录");
        }
        UserDto userDto = new UserDto();
        userDto.setUserId(userId);
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);

        Assert.listOnlyOne(userDtos, "未包含用户");
        return userDtos.get(0);
    }


    /**
     * 查询商户信息
     *
     * @param context
     * @return
     */
    protected StoreDto getStoreDto(ICmdDataFlowContext context) {
        String storeId = CmdContextUtils.getStoreId(context);
        Assert.hasLength(storeId, "未查询到商户");
        if (storeId.startsWith("-")) {
            throw new IllegalArgumentException("未查询到商户");
        }
        StoreDto storeDto = new StoreDto();
        storeDto.setStoreId(storeId);
        List<StoreDto> storeDtos = storeV1InnerServiceSMOImpl.queryStores(storeDto);

        Assert.listOnlyOne(storeDtos, "未包含商户");
        return storeDtos.get(0);
    }



    @Override
    public int getOrder() {
        return 0;
    }

}
